home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / iritsm3s.zip / IRIT.C < prev    next >
C/C++ Source or Header  |  1992-02-24  |  16KB  |  458 lines

  1. /*****************************************************************************
  2. *   Main module of "Irit" - the 3d polygonal solid modeller.             *
  3. ******************************************************************************
  4. * Usage:                                     *
  5. *   Irit [-t] [-z]                                 *
  6. *                                         *
  7. * Written by:  Gershon Elber                Ver 3.0, Apr. 1990   *
  8. *****************************************************************************/
  9.  
  10. #ifdef __MSDOS__
  11. #include <stdlib.h>
  12. #include <conio.h>
  13. #include <dir.h>
  14. #include <dos.h>
  15. #endif /* __MSDOS__ */
  16.  
  17. #include <stdio.h>
  18. #include <signal.h>
  19. #include <string.h>
  20. #include "program.h"
  21. #include "config.h"
  22. #include "ctrl-brk.h"
  23. #include "dataprsr.h"
  24. #include "dosintr.h"
  25. #include "graphgen.h"
  26. #include "inptprsg.h"
  27. #include "matherr.h"
  28. #include "objects.h"
  29. #include "windows.h"
  30.  
  31. #ifdef NO_CONCAT_STR
  32. static char *VersionStr = "Irit        Vertion 3.0, Gershon Elber,\n\
  33.     (C) Copyright 1989/90/91/92 Gershon Elber, Non commercial use only.";
  34. #else
  35. static char *VersionStr = "Irit            " VERSION
  36.     ",    Gershon Elber,    " __DATE__ ",   " __TIME__ "\n"
  37.     "(C) Copyright 1989/90/91/92 Gershon Elber, Non commercial use only.";
  38. #endif /* NO_CONCAT_STR */
  39.  
  40. static char *CtrlStr = "Irit [-t] [-z] [file.irt]";
  41.  
  42. char *GlblHelpFileName = "irit.hlp";
  43.  
  44. static char
  45.      *GlblPrgmHeader = "                    Irit - the polygonal solid modeller";
  46. static char
  47.      *GlblCopyRight  =
  48. #ifdef __MSDOS__
  49.      "(C) Copyright 1989/90/91/92 Gershon Elber,  IBMPC " VERSION ",   "
  50.         __DATE__;
  51. #else
  52. #ifdef DJGCC
  53.      "(C) Copyright 1989/90/91/92 Gershon Elber,  DJGCC " VERSION ",   "
  54.         __DATE__;
  55. #else
  56.      "(C) Copyright 1989/90/91/92 Gershon Elber,  Unix  Version 3.0";
  57. #endif /* DJGCC */
  58. #endif /* __MSDOS__ */
  59. static char
  60.      *GlblAuthorName = "                         Written by Gershon Elber";
  61.  
  62. ObjectStruct *GlblObjList = NULL;       /* All objects defined on system. */
  63.  
  64. char GlblCrntWorkingDir[LINE_LEN];     /* Save start CWD to recover on exit. */
  65.  
  66. FILE *GlblLogFile = NULL;
  67.  
  68. jmp_buf GlblLongJumpBuffer;                  /* Used in error recovery. */
  69.  
  70. /* The following are setable variables (via configuration file poly3d-h.cfg).*/
  71. #ifdef __MSDOS__
  72. static char
  73.     *SVGANameMode = "",
  74.     *BGIDriverPath = "";
  75. static int
  76.     GraphDriver = DETECT;
  77. #else
  78.  
  79. static int
  80.     PopTransAlways = FALSE;
  81. #endif /* __MSDOS__ */
  82.  
  83. int
  84. #if defined(__MSDOS__) || defined(DJGCC)     /* Defaults for MSDOS intr_lib. */
  85.     GlblWindowFrameWidth = 8,
  86.     GlblViewFrameColor   = INTR_COLOR_RED,
  87.     GlblViewBackColor    = INTR_COLOR_BLACK,
  88.     GlblTransFrameColor  = INTR_COLOR_GREEN,
  89.     GlblTransBackColor   = INTR_COLOR_BLACK,
  90.     GlblStatusFrameColor = INTR_COLOR_MAGENTA,
  91.     GlblStatusBackColor  = INTR_COLOR_BLACK,
  92.     GlblInputFrameColor  = INTR_COLOR_YELLOW,
  93.     GlblInputBackColor   = INTR_COLOR_BLACK,
  94.     GlblDrawHeader       = FALSE,
  95.     GlblSmoothTextScroll = TRUE,
  96.     GlblIntrSaveMethod   = INTR_SAVE_DISK,
  97.     GlblMouseSensitivity = 10,       /* Sensitivity control of mouse device. */
  98.     GlblJoystickExists   = FALSE,
  99. #endif /*__MSDOS__ || DJGCC */
  100.     GlblMouseExists      = TRUE,
  101.     GlblLoadColor        = DEFAULT_LOAD_COLOR,
  102.     GlblBoolColor        = DEFAULT_BOOL_COLOR,
  103.     GlblICrvColor        = DEFAULT_ICRV_COLOR,
  104.     GlblPrimColor        = DEFAULT_PRIM_COLOR,
  105.     GlblDoGraphics       = TRUE,/* Control if running in graphics/text mode. */
  106.     GlblFatalError       = FALSE, /* True if disaster in system - must quit! */
  107.     GlblTransformMode    = TRANS_SCREEN,       /* Screen/Object trans. mode. */
  108.     GlblViewMode         = VIEW_ORTHOGRAPHIC,         /* Persp/Ortho etc. */
  109.     GlblDepthCue         = TRUE,           /* Activate depth cueing. */
  110.     GlblDrawSolid        = FALSE,     /* Use hardware Z buffer rendering. */
  111.     GlblPrintLogFile     = FALSE;    /* If TRUE everything goes to log file. */
  112.  
  113. char
  114. #if defined(__MSDOS__) || defined(DJGCC)     /* Defaults for MSDOS intr_lib. */
  115.     *GlblViewWndwPos   = "0.02, 0.02, 0.72, 0.66",
  116.     *GlblTransWndwPos  = "0.75, 0.02, 0.98, 0.66",
  117.     *GlblStatusWndwPos = "0.75, 0.02, 0.98, 0.66",
  118.     *GlblInputWndwPos  = "0.04, 0.7,  0.98, 0.98",
  119.     *GlblIntrSaveDisk  = "c:\\",
  120. #endif /*__MSDOS__ || DJGCC */
  121. #ifdef __GL__
  122.     /* Preferance position and size of view and transformation windows. */
  123.     *GlblTransPrefPos = "455, 640, 520, 965",
  124.     *GlblViewPrefPos  = "1,   450, 520, 965",
  125. #endif /* __GL__ */
  126.     *GlblStartFileName = "",
  127.     *GlblLogFileName = "",
  128.     *GlblEditPrgm = "";
  129.  
  130. static ConfigStruct SetUp[] =
  131. {
  132. #if defined(__MSDOS__) || defined(DJGCC)
  133.   { "Joystick",        (VoidPtr) &GlblJoystickExists,    SU_BOOLEAN_TYPE },
  134.   { "Mouse",        (VoidPtr) &GlblMouseExists,    SU_BOOLEAN_TYPE },
  135.   { "MouseSensitivity",    (VoidPtr) &GlblMouseSensitivity,SU_INTEGER_TYPE },
  136.   { "WndwWidth",    (VoidPtr) &GlblWindowFrameWidth,SU_INTEGER_TYPE },
  137.   { "WndwHeader",    (VoidPtr) &GlblDrawHeader,    SU_BOOLEAN_TYPE },
  138.   { "WndwSmthTxtScrl",    (VoidPtr) &GlblSmoothTextScroll,SU_BOOLEAN_TYPE },
  139.   { "WndwViewClr",    (VoidPtr) &GlblViewFrameColor,    SU_INTEGER_TYPE },
  140.   { "WndwTransClr",    (VoidPtr) &GlblTransFrameColor,    SU_INTEGER_TYPE },
  141.   { "WndwInputClr",    (VoidPtr) &GlblInputFrameColor,    SU_INTEGER_TYPE },
  142.   { "WndwViewPos",    (VoidPtr) &GlblViewWndwPos,    SU_STRING_TYPE },
  143.   { "WndwTransPos",    (VoidPtr) &GlblStatusWndwPos,    SU_STRING_TYPE },
  144.   { "WndwInputPos",    (VoidPtr) &GlblInputWndwPos,    SU_STRING_TYPE },
  145.   { "WndwBackSave",    (VoidPtr) &GlblIntrSaveMethod,    SU_INTEGER_TYPE },
  146.   { "WndwBackSavePath",    (VoidPtr) &GlblIntrSaveDisk,    SU_STRING_TYPE },
  147. #else
  148.   { "PopTransAlways",   (VoidPtr) &PopTransAlways,    SU_BOOLEAN_TYPE },
  149. #endif /* __MSDOS__ || DJGCC */
  150. #ifdef __MSDOS__
  151.   { "SVGANameMode",    (VoidPtr) &SVGANameMode,    SU_STRING_TYPE },
  152.   { "BGIDriverPath",    (VoidPtr) &BGIDriverPath,    SU_STRING_TYPE },
  153.   { "GraphDriver",    (VoidPtr) &GraphDriver,        SU_INTEGER_TYPE },
  154.   { "WndwStatusClr",    (VoidPtr) &GlblStatusFrameColor,SU_INTEGER_TYPE },
  155.   { "WndwStatusPos",    (VoidPtr) &GlblStatusWndwPos,    SU_STRING_TYPE },
  156. #endif /* __MSDOS__ */
  157. #ifdef __GL__
  158.   { "TransPrefPos",    (VoidPtr) &GlblTransPrefPos,    SU_STRING_TYPE },
  159.   { "ViewPrefPos",    (VoidPtr) &GlblViewPrefPos,    SU_STRING_TYPE },
  160. #endif /* __GL__ */
  161.   { "DoGraphics",    (VoidPtr) &GlblDoGraphics,    SU_BOOLEAN_TYPE },
  162.   { "LoadColor",    (VoidPtr) &GlblLoadColor,    SU_INTEGER_TYPE },
  163.   { "BoolColor",    (VoidPtr) &GlblBoolColor,    SU_INTEGER_TYPE },
  164.   { "ICrvColor",    (VoidPtr) &GlblICrvColor,    SU_INTEGER_TYPE },
  165.   { "PrimColor",    (VoidPtr) &GlblPrimColor,    SU_INTEGER_TYPE },
  166.   { "StartFile",    (VoidPtr) &GlblStartFileName,    SU_STRING_TYPE },
  167.   { "LogFile",        (VoidPtr) &GlblLogFileName,    SU_STRING_TYPE },
  168.   { "EditPrgm",        (VoidPtr) &GlblEditPrgm,    SU_STRING_TYPE },
  169. };
  170. #define NUM_SET_UP    (sizeof(SetUp) / sizeof(ConfigStruct))
  171.  
  172. #ifdef __MSDOS__
  173. extern unsigned int _stklen = 16384;         /* Increase default stack size. */
  174. #endif /* __MSDOS__ */
  175.  
  176. static void Interact(void);
  177. static void PrintInptPrsrError(void);
  178.  
  179. /*****************************************************************************
  180. * Main routine - Read Parameter    line and do what you need...             *
  181. *****************************************************************************/
  182. void
  183. #ifdef __MSDOS__
  184. cdecl        /* So we can use -rp in Borland 3.0 (parameters in registers.). */
  185. #endif /* __MSDOS__ */
  186. main(int argc, char **argv)
  187. {
  188.     char *FullPathStartFileName;
  189.  
  190.     Config("irit", SetUp, NUM_SET_UP);       /* Read config. file if exists. */
  191.  
  192.     while (argc >= 2) {
  193.         if (strncmp(argv[1], "-z", 2) == 0) {
  194.         fprintf(stderr, "\n%s\n\nUsage: %s\n", VersionStr, CtrlStr);
  195.         ConfigPrint(SetUp, NUM_SET_UP);
  196.         exit(0);
  197.         }
  198.     else if (strncmp(argv[1], "-t", 2) == 0) {
  199.         GlblDoGraphics = FALSE;
  200.     }
  201.     else {
  202.         break;
  203.     }
  204.         argv++;
  205.     argc--;
  206.     }
  207.  
  208.     getcwd(GlblCrntWorkingDir, LINE_LEN - 1);
  209.  
  210.     SetUpCtrlBrk();         /* Set up control break trap routine (int 1bh). */
  211.     SetUpHardErr();        /* Set up hardware error trap routine (int 24h). */
  212.     signal(SIGFPE, DefaultFPEHandler);     /* Will trap floating point errors. */
  213.  
  214.     if (GlblDoGraphics) {
  215. #    ifdef __MSDOS__
  216.         GGSetStatusInputWindows(TRUE, TRUE);
  217.         GGInstallBGI(BGIDriverPath, SVGANameMode);
  218.         GGInitGraph(GraphDriver, FALSE);
  219. #    else
  220. #    ifdef DJGCC
  221.         GGSetStatusInputWindows(FALSE, TRUE);
  222.         GGInitGraph(0, FALSE);
  223. #    else
  224.         GGInitGraph(argc, argv, FALSE, PopTransAlways);
  225. #       endif /* DJGCC */
  226. #    endif /* __MSDOS__ */
  227.     }
  228.  
  229.     /* Print some copyright messages: */
  230.     WndwInputWindowPutStr(GlblPrgmHeader);
  231.     WndwInputWindowPutStr(GlblCopyRight);
  232.     WndwInputWindowPutStr(GlblAuthorName);
  233.  
  234. #ifdef __MSDOS__
  235.     DosGetTime(1.0);                    /* Reset the time count. */
  236. #endif /* __MSDOS__ */
  237.     SetUpPredefObjects();          /* Prepare the predefined objects. */
  238.  
  239.     AliasReset();
  240.  
  241.     /* Execute the file specified in the command line if was one: */
  242.     if (argc == 2) FileInclude(argv[1]);
  243.  
  244.     /* Execute the start up file first by inserting it to the include stack. */
  245.     if ((int) strlen(GlblStartFileName) > 0 &&
  246.     (FullPathStartFileName = searchpath(GlblStartFileName)) != NULL)
  247.     FileInclude(FullPathStartFileName);
  248.  
  249.     Interact();                      /* Go and do some real work... */
  250.  
  251.     MyExit(0);
  252. }
  253.  
  254. /*****************************************************************************
  255. * Interact - the main read/eval/print routine. This routine reads data from  *
  256. * standart input and execute it for ever (using Input Parser).             *
  257. * Note exit from this program is controled by input parser itself.         *
  258. *****************************************************************************/
  259. static void Interact(void)
  260. {
  261.     /* setjmp return 0 on first install time. Will return 1 if error is      */
  262.     /* recoverable, 2 if cannt continue - must quit the program now.         */
  263.     switch (setjmp(GlblLongJumpBuffer)) {         /* Used in error recovery. */
  264.     case 0:
  265.     case 1:
  266.         while (TRUE) {
  267.         if (!InputParser())         /* Print the error message. */
  268.             PrintInptPrsrError();
  269.         }
  270.     case 2:
  271.         WndwInputWindowPutStr("Press return to die...");
  272. #ifdef __MSDOS__
  273.         while (kbhit()) getch();        /* Flush any old keystrokes. */
  274.         getch();
  275. #else
  276.         getchar();
  277. #endif /* __MSDOS__ */
  278.         break;
  279.     }
  280. }
  281.  
  282. /*****************************************************************************
  283. * Routine to query (and print) the errors found in InputParser:             *
  284. *****************************************************************************/
  285. static void PrintInptPrsrError(void)
  286. {
  287.     InptPrsrEvalErrType ErrorNum;
  288.     char *ErrorMsg, *p;
  289.     char Line[LINE_LEN_LONG];
  290.  
  291.     if ((ErrorNum = InptPrsrParseError(&ErrorMsg)) != IPE_NO_ERR) {/*Prsr err*/
  292.     sprintf(Line, "Parsing Error: ");
  293.     p = &Line[strlen(Line)];
  294.     switch (ErrorNum) {
  295.         case IP_ERR_WRONG_SYNTAX:
  296.         sprintf(p, "Wrong syntax\n");
  297.         break;
  298.         case IP_ERR_PARAM_EXPECT:
  299.         sprintf(p, "Parameter Expected\n");
  300.         break;
  301.         case IP_ERR_ONE_OPERAND:
  302.         case IP_ERR_TWO_OPERAND:
  303.         sprintf(p, "Wrong # of operands - %s\n", ErrorMsg);
  304.         break;
  305.         case IP_ERR_STACK_OV:
  306.         sprintf(p, "Internal Stack OverFlow at - %s\n", ErrorMsg);
  307.         break;
  308.         case IP_ERR_PARAM_MATCH:
  309.         sprintf(p, "Parenthesis mismatch - %s\n", ErrorMsg);
  310.         break;
  311.         case IP_ERR_UNDEF_TOKEN:
  312.         sprintf(p, "Undefined token - %s\n", ErrorMsg);
  313.         break;
  314.         case IP_ERR_UNDEF_FUNC:
  315.         sprintf(p, "Undefined function - %s\n", ErrorMsg);
  316.         break;
  317.         case IP_ERR_NAME_TOO_LONG:
  318.         sprintf(p, "Object name too long - %s\n", ErrorMsg);
  319.         break;
  320.         case IP_ERR_PARAM_FUNC:
  321.         sprintf(p, "Parameters expected in func %s\n", ErrorMsg);
  322.         break;
  323.         case IP_ERR_NO_PARAM_FUNC:
  324.         sprintf(p, "No Parameters expected in func %s\n", ErrorMsg);
  325.         break;
  326.         default:
  327.         sprintf(p, "Undefined error %d", ErrorNum);
  328.         break;
  329.     }
  330.     WndwInputWindowPutStr(Line);
  331.     return;
  332.     }
  333.  
  334.     if ((ErrorNum = InptPrsrEvalError(&ErrorMsg)) != IPE_NO_ERR) {/*Eval err.*/
  335.     sprintf(Line, "Eval Error: ");
  336.     p = &Line[strlen(Line)];
  337.     switch (ErrorNum) {
  338.         case IE_ERR_FATAL_ERROR:
  339.         sprintf(p, "Fatal error - %s\n", ErrorMsg);
  340.         break;
  341.         case IE_ERR_DIV_BY_ZERO:
  342.         sprintf(p, "Division by zero - %s\n", ErrorMsg);
  343.         break;
  344.         case IE_ERR_NO_OBJ_METHOD:
  345.         sprintf(p, "No such method for object - %s\n", ErrorMsg);
  346.         break;
  347.         case IE_ERR_TYPE_MISMATCH:
  348.         sprintf(p, "Parameter type mismatch - %s\n", ErrorMsg);
  349.         break;
  350.         case IE_ERR_ASSIGN_LEFT_OP:
  351.         sprintf(p, "Lval is not a parameter - %s\n", ErrorMsg);
  352.         break;
  353.         case IE_ERR_MIXED_OBJ:
  354.         sprintf(p, "Mixed types in expression - %s\n", ErrorMsg);
  355.         break;
  356.         case IE_ERR_UNDEF_OBJECT:
  357.         sprintf(p, "No such object defined - %s\n", ErrorMsg);
  358.         break;
  359.         case IE_ERR_NO_ASSIGNMENT:
  360.         sprintf(p, "Assignment was expected\n");
  361.         break;
  362.         case IE_ERR_FP_ERROR:
  363.         sprintf(p, "Floating Point Error - %s\n", ErrorMsg);
  364.         break;
  365.         case IE_ERR_NUM_PRM_MISMATCH:
  366.         sprintf(p, "Number of func. param. mismatch - %s\n", ErrorMsg);
  367.         break;
  368.         case IE_ERR_MAT_POWER:
  369.         sprintf(p, "Wrong range or not exists, operator - %s\n", ErrorMsg);
  370.         break;
  371.         case IE_ERR_FREE_SIMPLE:
  372.         sprintf(p, "Free only geometric objects - %s\n", ErrorMsg);
  373.         break;
  374.         case IE_ERR_MODIF_ITER_VAR:
  375.         sprintf(p, "Iteration var. type modified or freed - %s\n", ErrorMsg);
  376.         break;
  377.         case IE_ERR_BOOLEAN_ERR:
  378.         sprintf(p, "Geometric Boolean operation error - %s\n", ErrorMsg);
  379.         break;
  380.         case IE_ERR_LIST_TOO_LONG:
  381.         sprintf(p, "List length too big - up to %d only.\n", MAX_OBJ_LIST);
  382.         break;
  383.         case IE_ERR_DATA_PRSR_ERROR:
  384.         sprintf(p, "%s", ErrorMsg);
  385.         break;
  386.         default:
  387.         sprintf(p, "Undefined error %d\n", ErrorNum);
  388.         break;
  389.     }
  390.     WndwInputWindowPutStr(Line);
  391.     return;
  392.     }
  393. }
  394.  
  395. /*****************************************************************************
  396. * Exit routine - called when this program should terminate.             *
  397. *****************************************************************************/
  398. void MyExit(int ExitCode)
  399. {
  400.     if (GlblDoGraphics) {
  401.         GGCloseGraph();                /* Close the graphic driver. */
  402.     }
  403.  
  404.     RestoreCtrlBrk();                  /* Restore ctrl-brk interrupt. */
  405.  
  406.     chdir(GlblCrntWorkingDir);      /* Recover original directory before exit. */
  407. #ifdef __MSDOS__
  408.     setdisk(GlblCrntWorkingDir[0] - 'A');        /* Move to the old disk. */
  409. #endif /* __MSDOS__ */
  410.  
  411.     if (GlblPrintLogFile) fclose(GlblLogFile);/* Close log file if was open. */
  412.  
  413.     exit(ExitCode);
  414. }
  415.  
  416. /*****************************************************************************
  417. * Trap Cagd_lib errors right here.                         *
  418. *****************************************************************************/
  419. void CagdFatalError(CagdFatalErrorType ErrID)
  420. {
  421.     char Line[LINE_LEN],
  422.     *ErrorMsg = CagdDescribeError(ErrID);
  423.  
  424.     sprintf(Line, "CAGD_LIB: %s", ErrorMsg);
  425.     WndwInputWindowPutStr(Line);
  426.  
  427.     longjmp(GlblLongJumpBuffer, 1);        /* Go back to menu directly. */
  428. }
  429.  
  430. /*****************************************************************************
  431. * FatalEror routine. print a fatal error message and go back to cursor mode  *
  432. *****************************************************************************/
  433. void FatalError(char *ErrorMsg)
  434. {
  435.     if (ErrorMsg != NULL) {
  436.     WndwInputWindowPutStr("Fatal error occured, please report it:");
  437.     WndwInputWindowPutStr(ErrorMsg);
  438.     }
  439.  
  440.     longjmp(GlblLongJumpBuffer, 1);        /* Go back to menu directly. */
  441. }
  442.  
  443. /*****************************************************************************
  444. *   Routine that is called from the floating point package in case of fatal  *
  445. * floating point error. Print error message, long jump to main loop. Default *
  446. * FPE handler - must be reset after redirected to other module.             *
  447. *****************************************************************************/
  448. #ifdef __MSDOS__
  449. void cdecl DefaultFPEHandler(int Sig, int Type, int *RegList)
  450. #else
  451. void DefaultFPEHandler(int Type)
  452. #endif /* __MSDOS__ */
  453. {
  454.     PrintFPError(Type);             /* Print the floating point error type. */
  455.  
  456.     longjmp(GlblLongJumpBuffer, 1);
  457. }
  458.